package com.rockwell.sniffyHunter.test;

import junit.framework.Assert;

import com.rockwell.sniffyhunter.model.NorthernStarFinder;
import com.rockwell.sniffyhunter.utils.Vector2D;

import android.graphics.PointF;
import android.test.AndroidTestCase;

public class NorthernStarFinderTester extends AndroidTestCase {
	private NorthernStarFinder 	gps;
	Vector2D 					location;
	
	@Override
	public void setUp() {
		gps = new NorthernStarFinder();
	}
	
	// Rotate to +90 degree off from North
	public void testRotate90() {
		gps.Rotate(90);
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(0.0f, 0.0f));
		Assert.assertEquals(location.angleFromNorth, 90);
	}
	
	// Rotate to -270 degree off from North
	public void testRotateMin270() {
		gps.Rotate(-270);
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(0.0f, 0.0f));
		Assert.assertEquals(location.angleFromNorth, 90);
	}
	
	// Rotate to +180 degree off from North
	public void testRotate180() {
		gps.Rotate(180);
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(0.0f, 0.0f));
		Assert.assertEquals(location.angleFromNorth, 180);
	}
	
	// Rotate to -180 degree off from North
	public void testRotateMin180() {
		gps.Rotate(-180);
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(0.0f, 0.0f));
		Assert.assertEquals(location.angleFromNorth, 180);
	}
	
	// Rotate to +270 degree off from North
	public void testRotate270() {
		gps.Rotate(270);
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(0.0f, 0.0f));
		Assert.assertEquals(location.angleFromNorth, 270);
	}
	
	// Rotate to -90 degree off from North
	public void testRotateMin90() {
		gps.Rotate(-90);
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(0.0f, 0.0f));
		Assert.assertEquals(location.angleFromNorth, 270);
	}
	
	// Rotate to +45 degree off from North (1st Quadrant)
	public void testRotate45() {
		gps.Rotate(45);
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(0.0f, 0.0f));
		Assert.assertEquals(location.angleFromNorth, 45);

		// Move to [(x,y), deg] = [(1,1), 45];		
		gps.MoveForward(Math.sqrt(2.0));
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(1.0f, 1.0f));
		Assert.assertEquals(location.angleFromNorth, 45);
	}	
	
	// Rotate to -315 degree off from North (1st Quadrant)
	public void testRotateMin315() {
		gps.Rotate(-315);
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(0.0f, 0.0f));
		Assert.assertEquals(location.angleFromNorth, 45);

		// Move to [(x,y), deg] = [(1,1), 45];		
		gps.MoveForward(Math.sqrt(2.0));
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(1.0f, 1.0f));
		Assert.assertEquals(location.angleFromNorth, 45);
	}
	
	// Rotate to +135 degree off from North (2nd Quadrant)
	public void testRotate135() {
		gps.Rotate(135);
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(0.0f, 0.0f));
		Assert.assertEquals(location.angleFromNorth, 135);
		
		// Move to [(x,y), deg] = [(1,-1), 135];		
		gps.MoveForward(Math.sqrt(2.0));
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(1.0f, -1.0f));
		Assert.assertEquals(location.angleFromNorth, 135);
	}
	
	// Rotate to -225 degree off from North (2nd Quadrant)
	public void testRotateMin225() {
		gps.Rotate(-225);
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(0.0f, 0.0f));
		Assert.assertEquals(location.angleFromNorth, 135);
		
		// Move to [(x,y), deg] = [(1,-1), 135];		
		gps.MoveForward(Math.sqrt(2.0));
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(1.0f, -1.0f));
		Assert.assertEquals(location.angleFromNorth, 135);
	}
	
	// Rotate to +225 degree off from North (3rd Quadrant)
	public void testRotate225() {				
		gps.Rotate(225);
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(0.0f, 0.0f));
		Assert.assertEquals(location.angleFromNorth, 225);
		
		// Move to [(x,y), deg] = [(-1,-1), 225];		
		gps.MoveForward(Math.sqrt(2.0));
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(-1.0f, -1.0f));
		Assert.assertEquals(location.angleFromNorth, 225);
	}
	
	// Rotate to -135 degree off from North (3rd Quadrant)
	public void testRotateMin135() {				
		gps.Rotate(-135);
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(0.0f, 0.0f));
		Assert.assertEquals(location.angleFromNorth, 225);
		
		// Move to [(x,y), deg] = [(-1,-1), 225];		
		gps.MoveForward(Math.sqrt(2.0));
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(-1.0f, -1.0f));
		Assert.assertEquals(location.angleFromNorth, 225);
	}
	
	// Rotate to +315 degree off from North (4th Quadrant)
	public void testRotate315() {
		gps.Rotate(315);
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(0.0f, 0.0f));
		Assert.assertEquals(location.angleFromNorth, 315);
		
		// Move to [(x,y), deg] = [(-1,1), 315];		
		gps.MoveForward(Math.sqrt(2.0));
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(-1.0f, 1.0f));
		Assert.assertEquals(location.angleFromNorth, 315);
	}
	
	// Rotate to -45 degree off from North (4th Quadrant)
	public void testRotateMin45() {
		gps.Rotate(-45);
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(0.0f, 0.0f));
		Assert.assertEquals(location.angleFromNorth, 315);
		
		// Move to [(x,y), deg] = [(-1,1), 315];		
		gps.MoveForward(Math.sqrt(2.0));
		location = gps.WhereAmI();
		Assert.assertEquals(location.coord, new PointF(-1.0f, 1.0f));
		Assert.assertEquals(location.angleFromNorth, 315);
	}
	
	public void testNearestObstacle() {
		// Move to [(x,y), deg] = [(1,1), 45];		
		gps.Rotate(45);
		gps.MoveForward(Math.sqrt(2.0));
		gps.MapDiscoveredObstacle();
		
		// Move to [(x,y), deg] = [(2,2), 45];		
		gps.MoveForward(Math.sqrt(2.0));
		gps.MapDiscoveredObstacle();
		
		// Move to [(x,y), deg] = [(3,3), 45];		
		gps.MoveForward(Math.sqrt(2.0));
		
		Vector2D nearestPoint = gps.FindNearestObstacle();
		Assert.assertEquals(new PointF(2.0f, 2.0f), nearestPoint.coord);
		
		// Move to [(x,y), deg] = [(0,0), 225];		
		gps.Rotate(180);
		gps.MoveForward(Math.sqrt(2.0));
		gps.MoveForward(Math.sqrt(2.0));
		gps.MoveForward(Math.sqrt(2.0));
		
		nearestPoint = gps.FindNearestObstacle();
		Assert.assertEquals(new PointF(1.0f, 1.0f), nearestPoint.coord);
	}
	
	@Override
	public void tearDown() {
		gps = null;
		location = null;
	}
}
